package com.wxq.ams.backup.controller;

import com.wxq.ams.backup.config.BackupDataSourceProperties;
import com.wxq.ams.backup.constants.BackupConstants;
import com.wxq.ams.backup.service.MysqlBackupService;
import com.wxq.ams.backup.utils.FileUtils;
import com.wxq.ams.backup.utils.http.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.*;

@RestController
@RequestMapping("/backup")
@Api(value = "MySQL数据库备份还原控制器")
public class MysqlBackupController {
    @Autowired
    private MysqlBackupService mysqlBackupService;
    @Autowired
    BackupDataSourceProperties properties;

    @GetMapping("/backup")
    @ApiOperation(value = "MySQL数据库备份")
    public HttpResult backup() throws Exception {
        String host = properties.getHost();
        String username = properties.getUsername();
        String password = properties.getPassword();
        String database = properties.getDatabase();
        //String backupFolderName = BackupConstants.DEFAULT_BACKUP_NAME + "_" + (new SimpleDateFormat(BackupConstants.DATE_FORMAT)).format(new Date());
        String backupFolderPath = BackupConstants.BACKUP_FOLDER;
        String fileName = BackupConstants.BACKUP_FILE_NAME();
        try {
            if (mysqlBackupService.backup(host, username, password, backupFolderPath, fileName, database)) {
                return HttpResult.ok();
            } else {
                return HttpResult.error(500, "数据库备份失败");
            }
        } catch (Exception e) {
            return HttpResult.error(500, e.getMessage());
        }

    }

    @GetMapping("/restore")
    @ApiOperation(value = "MySQL数据库还原", notes = "传入备份的名称还原备份")
    public HttpResult restore(@RequestParam String name) {
        String host = properties.getHost();
        String userName = properties.getUsername();
        String password = properties.getPassword();
        String database = properties.getDatabase();
        String port = properties.getPort();
        String restoreFilePath = BackupConstants.RESTORE_FOLDER + File.separator + name;
        try {
            if (mysqlBackupService.restore(restoreFilePath, host, userName, password, port, database)) {
                return HttpResult.ok();
            } else {
                return HttpResult.error(500, "数据库还原失败");
            }
        } catch (Exception e) {
            return HttpResult.error(500, e.getMessage());
        }

    }

    @GetMapping("/findRecords")
    @ApiOperation(value = "查询出所有备份")
    public HttpResult findBackupRecords() {
        List<Map<String, Object>> backupRecords = new ArrayList<>();
        File restoreFolderFile = new File(BackupConstants.RESTORE_FOLDER);
        if (restoreFolderFile.exists()) {
            for (File file : restoreFolderFile.listFiles()) {
                Map<String, Object> bean = new HashMap<>();
                bean.put("name", file.getName());
                bean.put("title", file.getName());
                if (BackupConstants.DEFAULT_BACKUP_FILE_NAME.equals(file.getName())) {
                    bean.put("title", "系统默认备份");
                }
                backupRecords.add(bean);
            }
        }
        return HttpResult.ok(backupRecords);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除数据库备份", notes = "传入数据库备份名，删除数据库备份")
    public HttpResult deleteBackupRecord(@RequestParam String name) {
        if (BackupConstants.DEFAULT_BACKUP_FILE_NAME.equals(name)) {
            return HttpResult.error("系统默认备份无法删除!");
        }
        String restoreFilePath = BackupConstants.RESTORE_FOLDER + File.separator + name;
        try {
            FileUtils.deleteFile(new File(restoreFilePath));
        } catch (Exception e) {
            return HttpResult.error(500, e.getMessage());
        }
        return HttpResult.ok();
    }
}
